<?php

namespace common\models\kingdee;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\kingdee\PersonFamily;
use yii\db\Query;

/**
 * PersonFamilySearch represents the model behind the search form about `common\models\kingdee\PersonFamily`.
 */
class PersonFamilySearch extends PersonFamily
{
    /**
     * @var string 与本人关系
     */
    public $RELATION;

    /**
     * @var string 与本人关系
     */
    public $PERSONNAME;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['FID', 'FNAME', 'FCREATORID', 'FCREATETIME', 'FLASTUPDATEUSERID', 'FLASTUPDATETIME', 'FRELATIONID', 'FPOLITICALFACEID', 'FBIRTHDAY', 'FWORKUNIT', 'FDEGREEID', 'FOFFICEPHONE', 'FHOMEADDRESS_L1', 'FHOMEADDRESS_L2', 'FHOMEADDRESS_L3', 'FHOMEPHONE', 'FPERSONID', 'FHRORGUNITID', 'FCONTROLUNITID', 'FIDCARDNO', 'FNATIVEPLACE', 'FGRADUATESCHOOL', 'FFOLKID', 'FSPECIALTY', 'FSPECDUTY', 'FREMARK_L1', 'FREMARK_L2', 'FREMARK_L3', 'FZDY1', 'FZDY2', 'FZDY3', 'FZDY4', 'FZDY5', 'FZDY6', 'FZDY7', 'FZDY8', 'FZDY9', 'FZDY10', 'FZDY11', 'FREMARK', 'FPERSONELSTATUS', 'FNAME_L1', 'FNAME_L2', 'FNAME_L3', 'FNUMBER', 'FDESCRIPTION_L1', 'FDESCRIPTION_L2', 'FDESCRIPTION_L3', 'FSIMPLENAME', 'CFHKLXID', 'CFMZID', 'CFWHCDID', 'CFYJ', 'CFZC', 'CFZGTZID', 'CFZW', 'CFJTZZ', 'CFLXDH', 'CFCS'], 'safe'],
            [['FZDY12', 'FZDY13', 'CFJJLXR'], 'integer'],
            [['FZDY14', 'FZDY15'], 'number'],
            [['RELATION', 'PERSONNAME'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = PersonFamily::find()
            ->alias('PF')
//            ->from(PersonFamily::tableName().' as PF')
            ->leftJoin('T_HR_BDRELATION as BDR', 'BDR.FID = PF.FRELATIONID')
            ->leftJoin('T_BD_PERSON as TBP', 'TBP.FID = PF.FPERSONID')
            ->select([
                'RELATION' => 'BDR.FNAME_L2',
                'FID' => 'PF.FID',
                'FRELATIONID',
                'FNAME_L2' => 'PF.FNAME_L2',
                'FBIRTHDAY' => "to_char(PF.FBIRTHDAY , 'yyyy-MM-dd')",
                'FPERSONID' => 'PF.FPERSONID',
                'FIDCARDNO' => 'PF.FIDCARDNO',
                'FPOLITICALFACEID' => 'PF.FPOLITICALFACEID',
                'PERSONNAME' => 'TBP.FNAME_L2',
                'FWORKUNIT' => 'PF.FWORKUNIT',
                'FOFFICEPHONE' => 'PF.FOFFICEPHONE',
                'FHOMEPHONE' => 'PF.FHOMEPHONE',
                'CFZW' => 'PF.CFZW',
                'FNATIVEPLACE' => 'PF.FNATIVEPLACE',
            ])->asArray();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        //让这个列也支持排序
        $sort = $dataProvider->getSort();
        $sort->attributes['RELATION'] = [
            'asc' => ['BDR.FNAME_L2' => SORT_ASC],
            'desc' => ['BDR.FNAME_L2' => SORT_DESC],
        ];
        $sort->attributes['PERSONNAME'] = [
            'asc' => ['TBP.FNAME_L2' => SORT_ASC],
            'desc' => ['TBP.FNAME_L2' => SORT_DESC],
        ];
        $dataProvider->setSort($sort);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'FCREATETIME' => $this->FCREATETIME,
            'FLASTUPDATETIME' => $this->FLASTUPDATETIME,
//            'PF.FBIRTHDAY' => $this->FBIRTHDAY,
            'FZDY4' => $this->FZDY4,
            'FZDY5' => $this->FZDY5,
            'FZDY6' => $this->FZDY6,
            'FZDY12' => $this->FZDY12,
            'FZDY13' => $this->FZDY13,
            'FZDY14' => $this->FZDY14,
            'FZDY15' => $this->FZDY15,
            'CFJJLXR' => $this->CFJJLXR,
        ]);

        $query->andFilterWhere(['like', 'FID', $this->FID])
            ->andFilterWhere(['like', 'FNAME', $this->FNAME])
            ->andFilterWhere(['like', 'FCREATORID', $this->FCREATORID])
            ->andFilterWhere(['like', 'FLASTUPDATEUSERID', $this->FLASTUPDATEUSERID])
            ->andFilterWhere(['like', 'FRELATIONID', $this->FRELATIONID])
            ->andFilterWhere(['like', 'FPOLITICALFACEID', $this->FPOLITICALFACEID])
            ->andFilterWhere(['like', 'FWORKUNIT', $this->FWORKUNIT])
            ->andFilterWhere(['like', 'FDEGREEID', $this->FDEGREEID])
            ->andFilterWhere(['like', 'FOFFICEPHONE', $this->FOFFICEPHONE])
            ->andFilterWhere(['like', 'FHOMEADDRESS_L1', $this->FHOMEADDRESS_L1])
            ->andFilterWhere(['like', 'FHOMEADDRESS_L2', $this->FHOMEADDRESS_L2])
            ->andFilterWhere(['like', 'FHOMEADDRESS_L3', $this->FHOMEADDRESS_L3])
            ->andFilterWhere(['like', 'FHOMEPHONE', $this->FHOMEPHONE])
            ->andFilterWhere(['like', 'FPERSONID', $this->FPERSONID])
            ->andFilterWhere(['like', 'FHRORGUNITID', $this->FHRORGUNITID])
            ->andFilterWhere(['like', 'FCONTROLUNITID', $this->FCONTROLUNITID])
            ->andFilterWhere(['like', 'FIDCARDNO', $this->FIDCARDNO])
            ->andFilterWhere(['like', 'FNATIVEPLACE', $this->FNATIVEPLACE])
            ->andFilterWhere(['like', 'FGRADUATESCHOOL', $this->FGRADUATESCHOOL])
            ->andFilterWhere(['like', 'FFOLKID', $this->FFOLKID])
            ->andFilterWhere(['like', 'FSPECIALTY', $this->FSPECIALTY])
            ->andFilterWhere(['like', 'FSPECDUTY', $this->FSPECDUTY])
            ->andFilterWhere(['like', 'FREMARK_L1', $this->FREMARK_L1])
            ->andFilterWhere(['like', 'FREMARK_L2', $this->FREMARK_L2])
            ->andFilterWhere(['like', 'FREMARK_L3', $this->FREMARK_L3])
            ->andFilterWhere(['like', 'FZDY1', $this->FZDY1])
            ->andFilterWhere(['like', 'FZDY2', $this->FZDY2])
            ->andFilterWhere(['like', 'FZDY3', $this->FZDY3])
            ->andFilterWhere(['like', 'FZDY7', $this->FZDY7])
            ->andFilterWhere(['like', 'FZDY8', $this->FZDY8])
            ->andFilterWhere(['like', 'FZDY9', $this->FZDY9])
            ->andFilterWhere(['like', 'FZDY10', $this->FZDY10])
            ->andFilterWhere(['like', 'FZDY11', $this->FZDY11])
            ->andFilterWhere(['like', 'FREMARK', $this->FREMARK])
            ->andFilterWhere(['like', 'FPERSONELSTATUS', $this->FPERSONELSTATUS])
            ->andFilterWhere(['like', 'FNAME_L1', $this->FNAME_L1])
            ->andFilterWhere(['like', 'PF.FNAME_L2', $this->FNAME_L2])
            ->andFilterWhere(['like', 'FNAME_L3', $this->FNAME_L3])
            ->andFilterWhere(['like', 'FNUMBER', $this->FNUMBER])
            ->andFilterWhere(['like', 'FDESCRIPTION_L1', $this->FDESCRIPTION_L1])
            ->andFilterWhere(['like', 'FDESCRIPTION_L2', $this->FDESCRIPTION_L2])
            ->andFilterWhere(['like', 'FDESCRIPTION_L3', $this->FDESCRIPTION_L3])
            ->andFilterWhere(['like', 'FSIMPLENAME', $this->FSIMPLENAME])
            ->andFilterWhere(['like', 'CFHKLXID', $this->CFHKLXID])
            ->andFilterWhere(['like', 'CFMZID', $this->CFMZID])
            ->andFilterWhere(['like', 'CFWHCDID', $this->CFWHCDID])
            ->andFilterWhere(['like', 'CFYJ', $this->CFYJ])
            ->andFilterWhere(['like', 'CFZC', $this->CFZC])
            ->andFilterWhere(['like', 'CFZGTZID', $this->CFZGTZID])
            ->andFilterWhere(['like', 'CFZW', $this->CFZW])
            ->andFilterWhere(['like', 'CFJTZZ', $this->CFJTZZ])
            ->andFilterWhere(['like', 'CFLXDH', $this->CFLXDH])
            ->andFilterWhere(['like', 'CFCS', $this->CFCS]);
        $query->andFilterWhere(['like', 'BDR.FNAME_L2', $this->RELATION]);
        $query->andFilterWhere(['like', 'TBP.FNAME_L2', $this->PERSONNAME]);
        $query->andFilterWhere(['like', "to_char(PF.FBIRTHDAY , 'yyyy-MM-dd')", $this->FBIRTHDAY]);

        return $dataProvider;
    }

//    public function attributeLabels()
//    {
//        $returnArr             = parent::attributeLabels();
//        $returnArr['RELATION'] = Yii::t('app', 'Frelationid');
//
//        return $returnArr;
//    }
}
